#!/usr/bin/env python39
# -*- coding:utf-8 -*-
from copyheaders import headers_raw_to_dict
from requests_html import HTMLSession

str = b'''
cookie: _gid=GA1.2.987852325.1649815881; _gat_gtag_UA_9095154_1=1; _gat_gtag_UA_9095154_30=1; Hm_lvt_ce70a1f49c2ca89b4b7f15662df921f2=1649815886; _gat_gtag_UA_9095154_9=1; _ga_QRLC25TVNF=GS1.1.1649815880.3.1.1649815918.0; _ga_7HV0R8DE69=GS1.1.1649815880.3.1.1649815918.0; Hm_lpvt_ce70a1f49c2ca89b4b7f15662df921f2=1649815918; _ga=GA1.1.1110223980.1641032133
referer: https://travel.xitek.com/photo/four-wheel/
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36
'''


class Demo:
    def __init__(self):
        self.session = HTMLSession()
        self.headers = headers_raw_to_dict(str)
        self.url = 'https://travel.xitek.com/headline/'
        self.detail_list = []

    def main(self):
        self.run()
        for url in self.detail_list:
            self.get_second_response(url)

    def run(self):
        '''
        主函数,发起请求获得响应
        :return:
        '''
        try:
            r = self.session.get(url=self.url, headers=self.headers)
            print(r.status_code, r.encoding)
            element_data = r.html.find('.a_zmi_li ul li a')
            for href in element_data:
                detail_url = 'https:' + href.attrs['href']
                self.detail_list.append(detail_url)
        except Exception as e:
            print('Error', e)

    def get_second_response(self, url):
        try:
            rr=self.session.get(url=url, headers=self.headers)
            print(rr.status_code, rr.encoding)
            detail_info = rr.html.find('.sam_body p')
            for detail in detail_info:
                print(detail.text)
            detail_img=rr.html.find('.sam_body p .w100')
            for img in detail_img:
                print(img.attrs['src'])
        except Exception as e:
            print('Error', e)



if __name__ == '__main__':
    demo = Demo()
    demo.main()
